/* 学习题库的组合函数 */
import type { BaseLoadQueryParams, LearnBaseTypeInfo, LearnBaseQuestionInfo, SelectedBase } from '@/api/base/types'
import { BaseTypeList } from '@/api/constants'

import { baseLoadApi, getSignsApi } from '@/api/base'


import { useLearnBaseInfoStore } from '@/stores/modules/learn_base_info'
import { useSignsStore } from '@/stores/modules/signs_store'
import { useInitKnowsExerciseProgress } from '@/composables/Exercise'
import { ref, watchEffect } from 'vue'

/**
 * 学习题库下载
 */
export const useLearnBaseLoad = async (
  baseLoadQueryParams: BaseLoadQueryParams = {
    driveCityCode: '4101',
    driveCode: 'xc',
    driveStage: 'wbm',
    driveType: 'jsz',
  },
) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  const signsStore = useSignsStore()

  if (!learnBaseInfoStore.learnBaseInfo.baseTypes) {
    // 调用接口下载学习题库
    const baseLoadResult = await baseLoadApi(baseLoadQueryParams)
    learnBaseInfoStore.setLearnBaseInfo(baseLoadResult.data)
    // 初始化考点练习
    let knowExercises = baseLoadResult.data.knowExercises
    useInitKnowsExerciseProgress(knowExercises!)
    // 初始化标志数据
    let signsResult = await getSignsApi()
    signsStore.setSigns(signsResult.data)
    console.log('题库不存在，需要下载')
  } else {
    console.log('题库已经存在，不需要下载')
  }
}

/**
 *清除题库信息
 */
export const clearLearnBaseInfo = () => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  const signsStore = useSignsStore()
  // 清除题库信息
  learnBaseInfoStore.clearLearnBaseInfo()
  // 清除标志数据
  signsStore.clearSigns()
}
/**
 * 学习题库的考试科目
 * @returns learnBaseTypes
 */
export const useLearnBaseTypes = () => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  const learnBaseTypes = ref<LearnBaseTypeInfo[]>()
  learnBaseTypes.value = learnBaseInfoStore.learnBaseTypes
  watchEffect(() => (learnBaseTypes.value = learnBaseInfoStore.learnBaseTypes))
  return {
    learnBaseTypes,
  }
}
/**
 * 学习题库试题相关的数量
 * @param baseType
 * @returns
 */
export const useLearnBaseQbCount = (baseType: string) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  const qbCount = ref<number>()
  const selectQbCount = ref<number>()
  watchEffect(() => {
    qbCount.value = learnBaseInfoStore.questionCount(baseType)
    selectQbCount.value = learnBaseInfoStore.selectLearnBaseQuestionCount(baseType)
  })

  return { qbCount, selectQbCount }
}

/**
 *
 * @param baseType 根据题库类型返回试题
 * @returns
 */
export const useQuestions = (baseType: string) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  const questions = ref<LearnBaseQuestionInfo[]>()
  watchEffect(() => {
    questions.value = learnBaseInfoStore.questions(baseType)
  })
  return { questions }
}

/**
 *精选题库信息
 * @param baseType
 * @returns
 */
export const useSelectedBase = (baseType: string) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  const selectedBase = ref<SelectedBase>()
  if (baseType === 'km1') {
    selectedBase.value = learnBaseInfoStore.learnBaseInfo.kmySelectedBase
  }
  else if (baseType === 'km4') {
    selectedBase.value = learnBaseInfoStore.learnBaseInfo.kmsSelectedBase
  }
  return selectedBase
}

/**
 * 城市信息
 */
export const useCityInfo = () => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  return learnBaseInfoStore.learnBaseInfo.cityInfo
}


/**
 * 是否是新规题
 * @param baseType
 * @param questionId
 */
export const useTopicIsNewRule = (baseType: string, questionId: number) => {

  let newRuleCode: string = ''
  if (baseType === 'km1') {
    newRuleCode = '1101'
  }
  else {
    newRuleCode = '4101'
  }
  const learnBaseInfoStore = useLearnBaseInfoStore()
  let newRule = learnBaseInfoStore.learnBaseInfo.specialExercises?.find(item => item.code === newRuleCode)
  if (newRule) {
    return newRule.questions!.findIndex(item => item === questionId) != -1
  }
  return false
}


/**
 * 是否是本地试题
 * @param baseType
 * @param classesCode
 * @returns
 */
export const useTopicIsLocate = (baseType: string, classesCode: string) => {

  const learnBaseInfoStore = useLearnBaseInfoStore()
  let cityCode = learnBaseInfoStore.learnBaseInfo.cityInfo?.code
  if (cityCode) {
    let regionCode: any = cityCode.substring(0, 2)
    let locateClasses = learnBaseInfoStore.learnBaseInfo.classes?.find(item => item.baseType == baseType && item.regionCode == regionCode)
    if (locateClasses) {
      return locateClasses.code === classesCode
    }

  }

  return false
}


/**
 * 本地试题类别
 * @param baseType
 * @returns
 */
export const useLocateClassesCode = (baseType: string) => {

  const learnBaseInfoStore = useLearnBaseInfoStore()
  let cityCode = learnBaseInfoStore.learnBaseInfo.cityInfo?.code
  if (cityCode) {
    let regionCode: any = cityCode.substring(0, 2)
    let locateClasses = learnBaseInfoStore.learnBaseInfo.classes?.find(item => item.baseType == baseType && item.regionCode == regionCode)
    if (locateClasses) {
      return locateClasses.code
    }

  }

  return undefined
}

/**
 * 题库类型名称
 * @param baseType
 */
export const useBaseTypeName = (baseType: string) => {
  return BaseTypeList.find(item => item.code === baseType)?.name
}

/**
 * 获取题库信息
 * @returns
 */
export const useBaseInfo = () => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  return learnBaseInfoStore.learnBaseInfo

}
